// this program processes the file "userpass.txt".

// does hashing by creating a hash table by userid.

// hash fucntion used is: took the ascii value of all characters and add them
// and  take mod30 of it.

// best case: when sum of ascii values of all the userid's are different.

//worst case:when sum of ascii values of all the userid's 
//are same(which is very rare).

// average case :when some have same sum of ascii values of characters.

//writing to a file has been in the commented mode to write the file on your own
// please decomment lines from 101-125.(IMPORTANT PLZ READ)

//the text file I created will be attached in the mail.

#include<stdio.h>

#include<string.h>
struct node			// tok a array of structure to store username and
					//password in the hash table.		
{
	char user[40];
	char pass[20];
	
}n[30];				//size of hash table is 30, hence maximum 
						//posssible enteries can be 30 as 
						//required by open addressing scheme.(alpha<1)
int h=0;
void search()			//to search for the password of the userid and to verify
{						//whether userid exits and other checkings.
	int q,j,sum,s,l;
	int i=0,k=0,flag=0;
	char password[20];
	char userid[40];
	printf("please enter the userid max length of 40 \n ");
	scanf("%s",&userid);
	sum=0;	
	for(s=0;s<strlen(userid);s++)	// getting the sum of asccii values 
									//of all chrachters
	{
			l=userid[s];
			sum=sum+l;
	}
	
	do 
	{
		j=(sum+i)%30;	//getting the index for the sum in the hash table
		if(strcmp(n[j].user,userid)==0)//checking whether userid entered is valid.
		{
			flag=1;    		//  variable set to 1 when userid matches
			while(k<3)   	//allowing maximum tries to be 3.
			{
				printf("enter the password\n");
				scanf("%s",password);
				if(strcmp(n[j].pass,password)==0)
				{
					printf("yor account has been opened\n");
					h=1;
					break;
					//k=5;		//just an aribitary value to get out of the loop
					
				}
				else
				{
					printf("you entered the wrong password\n");
					k++;
					if(k==3)		//too many tries access denied
					{
						printf("you cannot open ur account\n");
						h=1;
					}	
				}
				
				
			}
		}
		else
		{
			i++;
		}	
	}while(i!=30 && strcmp(n[j].user,"")!=0&& h!=1);/*loop continues till i!=30 and 
											 userid block inthe hash table
											 is not empty*/
	
	if(flag==0)								//if userid does not match
	{
		printf("the userid yo enterd could not be found\n");
	}
	
	
}
void main()
{

	int x,i,m,l,sum,s;
	FILE *fp,*fp1;
	char ch='y';
	char user[40];
	char pass[20];
	char userid[40];
	
	/*struct rec			// structure to write username and password in 
							//the text file. 
	{
		char user[40];
		char pass[20];
	};
	struct rec a;
	fp=fopen("userpass.txt","w");		//openig file in write mode.
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	// writing the file and prompting user to add another record or not.
	while(ch=='y'||ch=='Y')
	{
		
		printf("\nenter the username and password seperated by space");
		scanf("%s%s",&a.user,&a.pass);
		fprintf(fp,"%s %s\n",a.user,a.pass);
		printf("do you want to ad another username password(y/n)");
		fflush(stdin);
		scanf("%c",&ch);
		
	}
	fclose(fp);*/
	
	fp1=fopen("userpass.txt","r");	//opening the file inthe read mode
	if(fp==NULL)
	{
		
		puts("cannot open file");
		exit(1);
	}
	while(fscanf(fp1,"%s %s",user,pass)!=EOF)//scanning the recods from the file
											// till we encounter end of file
	{
		printf("%s %s\n",user,pass);	//printing the userid and password
		sum=0;
		for(s=0;s<strlen(user);s++)     //getting the sum of ascii values 
										//of all characters
		{
			l=user[s];
			sum=sum+l;
		}
		printf("sum of ascii values is :%d\n",sum); //printing the value of sum.
		
		
		i=0;
		while(i!=30)		//loop tiil 30 for probing at max 30 times.
		{
			//x=user[0];
			m=(sum+i)%30;
			if(strcmp(n[m].user,"")==0)//putting userid and password in the
										// hash table when block is empty.
			{
				//p[m]=&n[m];
				strcpy(n[m].user,user);
				strcpy(n[m].pass,pass);
				//printf("\n%s %s",n[m].user,n[m].pass);
				printf("index of hash table is :%d\n\n",m);//printing the 
														//index of the hsh table.
				break;	
			}
			else
			{
				i++;
			}
		
		}
	}
	fclose(fp1);//closing the file using file pointer.
	
	
	search();		//calling the search function to do the further process.
		
	
	
}